package com.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionConfig implements HandlerExceptionResolver {
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		if (ex instanceof NullPointerException) {
			System.out.println("空指针异常...");

		} else if (ex instanceof ArithmeticException) {
			System.out.println("算术异常...");
			ex.printStackTrace();
		} else {
			System.out.println("其他异常...");
			ex.printStackTrace();
		}

		ModelAndView mv = new ModelAndView();
		mv.addObject("errorMsg", ex.getMessage());
		mv.setViewName("error"); // templates/error.html

		return mv;
	}

}
